package com.codefish.framework.spring.aop.framework;

import com.codefish.framework.spring.aop.IMethodMatcher;
import com.codefish.framework.spring.aop.ITargetResource;
import org.aopalliance.intercept.MethodInterceptor;

import java.util.ArrayList;
import java.util.List;

/**
 * Aop理论中的Advise，包含了实现代理需要的一些信息
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/14 下午 03:01
 */
public class AdviseSupport {

    private ITargetResource targetResource;
    private final List<Class<?>> interfaces = new ArrayList<>();
    private final InterceptorAndDynamicMethodMatcher interceptorAndDynamicMethodMatcher = new InterceptorAndDynamicMethodMatcher();

    private boolean proxyTargetClass = false;


    public IMethodMatcher getMethodMatcher() {
        return this.interceptorAndDynamicMethodMatcher.getMethodMatcher();
    }

    public void setMethodMatcher(IMethodMatcher methodMatcher) {
        this.interceptorAndDynamicMethodMatcher.setMethodMatcher(methodMatcher);
    }


    public MethodInterceptor getMethodInterceptor() {
        return this.interceptorAndDynamicMethodMatcher.getMethodInterceptor();
    }

    public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
        this.interceptorAndDynamicMethodMatcher.setMethodInterceptor(methodInterceptor);
    }


    public void setTargetResource(ITargetResource targetResource) {
        this.targetResource = targetResource;
        for (Class<?> interfaceClazz : targetResource.targetClass().getInterfaces()) {
            addInterfaces(interfaceClazz);
        }
    }

    public ITargetResource getTargetResource() {
        return targetResource;
    }

    public boolean isProxyTargetClass() {
        return proxyTargetClass;
    }

    public void setProxyTargetClass(boolean proxyTargetClass) {
        this.proxyTargetClass = proxyTargetClass;
    }

    public Class<?>[] getInterfaces() {
        Class<?>[] array = new Class[interfaces.size()];
        interfaces.toArray(array);
        return array;
    }

    public Class<?> getTargetClass() {
        return this.targetResource.targetClass();
    }

    public void addInterfaces(Class<?> interfaceClazz) {
        interfaces.add(interfaceClazz);
    }
}
